// packageA/member/new_info_v2/new_info_v2.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false, //底部弹窗
    dataList: '',
    agreementShow: false,//协议弹窗
    userAgreement: '',//用户协议
    platformAgreement: '',//平台协议
    wechat_login_mode: false,
    popUp: '',//弹窗子组件
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      popUp: this.selectComponent('#popUp')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()
    let basic_info = wx.getStorageSync('yz_basic_info');
    this.setData({
      email_register:basic_info.plugin_setting?.email_register?.is_open || 0,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  confirm() {
    this.setData({
      show: false
    })
  },
  openPopUp(e) { //弹出下拉框
    this.data.popUp.checkShow()
    this.data.popUp.stateChanges(e.currentTarget.dataset.celltype)
    if(e.currentTarget.dataset.celltype == 'email'){
      this.data.popUp.email = '';
      if(this.data.dataList.email){
        this.data.popUp.setData({
          email:this.data.dataList.email
        })
      }
    }
  },

  getData() {
    let that = this
    var reg = /^(\d{6})\d+(\d{4})$/;
    let urlStr = app.getNetAddresss("member.member-info.user-info-update-page");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            dataList: res.data
          })
          if(!app._isTextEmpty(that.data.dataList.bank_card)){
            this.setData({
              'dataList.bank_card':that.data.dataList.bank_card.replace(reg, "$1******$2")
            })
          }
          that.data.popUp.assignment('alipayForm.alipay',res.data.alipay)
          that.data.popUp.assignment('alipayForm.alipayname',res.data.alipayname) 
          that.data.popUp.assignment('weChatForm.wechat',res.data.wechat)
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  loginOut(){
    wx.showModal({
      title: '提示',
      content: '确认退出当前账号？',
      complete: (res) => {
        if (res.confirm) {
          this.loginOutPost();
        }
      }
    })
  },
  loginOutPost(){
    let urlStr = app.getNetAddresss("member.logout.index");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          // 清除相关本地缓存
          wx.removeStorageSync('session');
          wx.removeStorageSync('nickname');
          wx.removeStorageSync('wx_token');
          wx.removeStorageSync('new_decorate');
          wx.removeStorageSync('openid');
          wx.removeStorageSync('userInfo');
          wx.removeStorageSync('yz_uid');
          wx.navigateTo({
            url: '/pages/login/login?isOut=1',
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMemberData() {
    let urlStr = app.getNetAddresss("member.member.member-data");
    app._getNetWork({
      url: urlStr,
      data: { v: 2, basic_info: 1 },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            wechat_login_mode: response.data.setting.wechat_login_mode
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

  //绑定手机
  bindTel() {
    if(this.data.dataList.change_mobile != 1 && this.data.dataList.mobile) return
    wx.navigateTo({
      url:
        '/packageA/member/editmobile/editmobile?num=' +
        this.data.dataList.mobile +
        '&fromApp=2&editType=1'
    });
    wx.setStorage({
      key: 'isset',
      data: 1
    });
  },

  agreementContent(e) { //获取协议内容
    let that = this
    let url = ''
    if (this.data.dataList.platform_agreement.status&&e=='platform') { //平台协议
      url = "member.member-info.platform-agreement"
    }else if(this.data.dataList.register_agreement.status&&e=='register'){ //用户协议
      url = "member.member-info.register-agreement"
    }
      let urlStr = app.getNetAddresss(url);
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.setData({
              platformAgreement: res.data,
              'platformAgreement.content':res.data.content.replace(/\<img/gi,'<img style ="max-width:100%;height:auto"')
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
  },
  getAgreement(e){ //打开协议弹窗
    this.setData({
      agreementShow:true
    })
    this.agreementContent(e.currentTarget.dataset.value)
  },

  closeAgreement(){ //关闭协议弹窗
    this.setData({
      agreementShow:false
    })
  },

  // 银行卡
  editBank() {
    wx.navigateTo({
      url: '/packageA/member/memberBank/memberBank'
    });
  },

   //余额支付密码设置
   editBalancePwd() {
    // let that = this;
    let urlStr = app.getNetAddresss('member.balance-password.is-has-password');
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.is_has) {
            wx.navigateTo({
              url: '/packageA/member/balance_password/balance_password'
            });
          } else {
            wx.navigateTo({
              url: '/packageA/member/set_balance_password/set_balance_password'
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1000
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

   // 用户信息
   userinfo() {
    wx.navigateTo({
      url: '/packageA/member/new_userinfo/new_userinfo'
    });
  },

  // 跳转汇聚账户信息
  convergenceSettlementCard() {
    wx.navigateTo({
      url: '/packageA/others/HJinfo/bankCardInformation/bankCardInformation'
    });
  },

  // 收货地址管理
  address() {
    wx.navigateTo({
      url: '/packageD/member/addressList/addressList'
    });
  },
  // 账号注销
  memberCancel () {
    wx.navigateTo({
      url: '/packageI/member_cancel/member_cancel'
    });
  },

   // 用户协议
   agreement() {
    wx.navigateTo({
      url: '/packageA/member/info/agreement/agreement?type=user'
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})